package cn.lhx.blog.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.lhx.blog.formbean.LinkFormBean;
import cn.lhx.blog.pojo.Link;
import cn.lhx.blog.service.LinksService;

public class LinksAction extends DispatchAction{
@Resource
private LinksService linksService;





@Override
protected ActionForward dispatchMethod(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response, String name)
		throws Exception {
	log.info("派遣方法："+name);
	if(null==name){
		return this.init(mapping, form, request, response);
	}else {
		return super.dispatchMethod(mapping, form, request, response, name);
	}
	
}


/**
 * 初始化
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
public ActionForward init(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response)
		throws Exception {
	//读取链接列表
	List<Link> links=linksService.getLinks(0, 10);
	HttpSession session=request.getSession();
	session.setAttribute("links", links);
	
	
	return mapping.findForward("initOK");
}
	

	/**
	 * 添加链接
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addLinks(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("添加链接");
		LinkFormBean lkf=(LinkFormBean) form;
		//new 一个 Link
		Link link=new Link();
		link.setDescription(lkf.getDescription());
		link.setName(lkf.getName());
		link.setUrl(lkf.getUrl());
		//添加
		linksService.addLink(link);
		return mapping.findForward("saveOK");
	}
	
	public ActionForward deleteLinks(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.info("删除链接");
		LinkFormBean lkf=(LinkFormBean) form;
		linksService.delLink(lkf.getId());
		return mapping.findForward("deleteOK");
	}
	
	
	

}
